﻿using Idenmpotency;
using System.Collections.Concurrent;
using Yarp.ReverseProxy.Idenmptency.Modles;

namespace Yarp.ReverseProxy.Idenmptency.Test.Idenmptency
{
    /// <summary>
    /// 用户定义的映射（该Yarp路由是否幂等）仓储
    /// </summary>
    public class AppYarpRouteIdenmpotencyRepository : IYarpRouteIdenmpotencyRepository
    {
        private static IDictionary<string, bool> _cache = new ConcurrentDictionary<string, bool>();
        static AppYarpRouteIdenmpotencyRepository()
        {
            _cache.Add("route1", true);
            _cache.Add("route2", false);
        }
        public AppYarpRouteIdenmpotencyRepository()
        {

        }
        public bool IsIdenmpotencyRoute(string routeId)
        {
            if (!_cache.ContainsKey(routeId)) throw new IdenmpotencyException($"路由{routeId}未注册");
            return _cache[routeId];
        }
        public void Save(YarpRouteIdenmpotency yarpRouteIdenmpotency)
        {
            _cache.Add(yarpRouteIdenmpotency.YarpRouteId, yarpRouteIdenmpotency.IsIdenmpotency);
        }
    }
}
